package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BedServiceImpl implements BedService {

    private final BedMapper bedMapper;

    /**
     * 通过房间ID检索床位
     * @param roomId 房间ID
     * @return 床位视图对象列表
     */
    public List<BedVo> getBedsByRoomId(@RequestParam Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 创建床位
     * @param bedDto 床位DTO
     * @return 创建的床位数量
     */
    public int addBed(@RequestBody BedDto bedDto) {
        Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);
        try {
            return bedMapper.insert(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    /**
     * 根据ID查询床位
     * @param id 床位ID
     * @return 床位视图对象
     */
    public BedVo getBedsByBedId(@PathVariable Long id) {
        Bed bed = bedMapper.selectById(id);
        return BeanUtil.copyProperties(bed, BedVo.class);
    }

    /**
     * 更新床位
     * @param bedDto 床位DTO
     * @return 更新的床位数量
     */
    public int updateBed(@RequestBody BedDto bedDto) {
        Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);
        try {
            return bedMapper.update(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_UPDATE_FAIL);
        }
    }

    /**
     * 删除床位
     * @param id 床位ID
     * @return 删除的床位数量
     */
    public int deleteBed(@PathVariable Long id) {
        return bedMapper.delete(id);
    }
}

